import Sprite = laya.display.Sprite
import Loader = laya.net.Loader
import Texture = laya.resource.Texture
import Handler = laya.utils.Handler
import Browser = laya.utils.Browser
import TiledMap = laya.map.TiledMap
import Rectangle = laya.maths.Rectangle
import Stat = laya.utils.Stat
import TextFiled = laya.display.Text
import LocalStorage = laya.net.LocalStorage

// 程序入口
class Game {
    gameLoadingUI: view.GameLoading
    gameStartUI: view.GameStart
    gameSelecteUI: view.GameSelect
    gameSceneUI: view.GameScene

    constructor() {
        //初始化微信小游戏
        Laya.MiniAdpter.init();
        //初始化引擎
        Laya.init(0, 0, true);
        Laya.stage.bgColor = "#494d42"

        // Laya.Stat.show();

        //适配模式
        Laya.stage.scaleMode = Laya.Stage.SCALE_FULL;
        Laya.stage.screenMode = Laya.Stage.SCREEN_HORIZONTAL;

        //加载资源
        this._load();
    }

    //加载资源
    _load(): void {
        //加载资源
        // var pro1:Object={url:"images/button_fullscreen.png", type:Loader.IMAGE}
        // var pro2:Object={url:"images/button_sound_on.png", type:Loader.IMAGE}
        // var pro3:Object={url:"images/button_sound_off.png", type:Loader.IMAGE}
        // var pro4:Object={url:"images/button_start.png", type:Loader.IMAGE}
        // var pro5:Object={url:"images/button_exit.png", type:Loader.IMAGE}

        // var proArr:Array<any>=[]
        // proArr.push(pro1, pro2, pro3, pro4, pro5)

        // Laya.loader.load(proArr, Laya.Handler.create(this, () => {
        //     this.gameStartScene()
        //     SoundPlayer.Instance.setMute(false)
        // }), Laya.Handler.create(this, (value:number) => {
        //     console.log(value)
        //     this.gameLoadingUI.progressLabel.text = "正在加载资源，请稍后..." + Math.floor(value*100) + "%"
        // }), Laya.Loader.IMAGE)

        Laya.loader.load("images/loader.png", Laya.Handler.create(this, () => {
            this.gameLoadingScene()
        }), null, Laya.Loader.IMAGE)
    }

    gameLoadingScene(): void {
        this.gameLoadingUI = new view.GameLoading()
        this.gameLoadingUI.progressLabel.text = "正在加载资源，请稍后..."
        Laya.stage.addChild(this.gameLoadingUI)

        var pro1:Object = { url: "res/atlas/comp.atlas", type: Loader.ATLAS}
        var pro2:Object = { url: "res/atlas/images.atlas", type: Loader.ATLAS}
        var proArr: Array<any> = []
        proArr.push(pro1, pro2)

        Laya.loader.load(proArr, Laya.Handler.create(this, () => {
            this.gameStartScene()
            SoundPlayer.Instance.setMute(false)
        }), Laya.Handler.create(this, (value:number) => {
            console.log(value)
            this.gameLoadingUI.progressLabel.text = Math.floor(value*100) + "%"
        }), Laya.Loader.ATLAS)
    }

    //游戏页面
    gameStartScene(): void {
        this.gameStartUI = new view.GameStart()
        this.gameStartUI.startBtn.on("click", this, this.gameSelectScene, [0])

        Laya.stage.addChild(this.gameStartUI)
        Laya.stage.removeChild(this.gameLoadingUI)
        this.gameLoadingUI.destroy()
    }

    gameSelectScene(): void {
        this.gameSelecteUI = new view.GameSelect()

        Laya.stage.addChild(this.gameSelecteUI)
        Laya.stage.removeChild(this.gameStartUI)
        this.gameStartUI.destroy()
    }

    gameStart(): void {
        Laya.stage.removeChild(this.gameStartUI)

        this.gameMain(1);
    }

    gameExit(): void {
        this.gameStartScene()
    }

    gameMain(stageNum: number): void {
        this.gameSceneUI = new view.GameScene()
        this.gameSceneUI.makeSceneUI(1)

        Laya.stage.addChild(this.gameSceneUI)
        Laya.stage.removeChild(this.gameStartUI)
        this.gameStartUI.destroy()
        Laya.stage.removeChild(this.gameSelecteUI)
        this.gameSelecteUI.destroy()        
    }    

}
let game = new Game();